﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Repository.Hierarchy;
using log4net.Appender;
using log4net.Layout;
using log4net;

namespace Celeriq.Utilities
{
	public static class Logger
	{
		#region Class Members

		private const string _eventSource = "Celeriq Core Services";
		private const string _eventLog = "Application";
		private static log4net.ILog _logger = null;

		#endregion

		#region Constructors

		static Logger()
		{
			_logger = log4net.LogManager.GetLogger("File");
		}

		#endregion

		#region Setup

		public static void Setup(string logFile, System.Type type)
		{
			try
			{
				var hierarchy = (Hierarchy)LogManager.GetRepository();
				hierarchy.Root.RemoveAllAppenders();

				var fileAppender = new FileAppender();
				fileAppender.AppendToFile = true;
				fileAppender.LockingModel = new FileAppender.MinimalLock();
				fileAppender.File = logFile;
				var pl = new PatternLayout();
				pl.ConversionPattern = "%d{yyyy-MM-dd HH:mm:ss} %p %m%n";
				pl.ActivateOptions();
				fileAppender.Layout = pl;
				fileAppender.Threshold = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).LevelMap["INFO"];
				fileAppender.ActivateOptions();

				log4net.Config.BasicConfigurator.Configure(fileAppender);
				_logger = LogManager.GetLogger(type);

			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		#endregion

		#region Logging

		public static void LogError(string message)
		{
			try
			{
				Console.WriteLine(message);
				if (_logger != null)
					_logger.Error(message);
				System.Diagnostics.EventLog.WriteEntry(_eventSource, message, System.Diagnostics.EventLogEntryType.Error);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		public static void LogError(Exception exception, string message)
		{
			try
			{
				Console.WriteLine(exception.ToString());
				if (_logger != null)
					_logger.Error(message + "\n" + exception.ToString());
				System.Diagnostics.EventLog.WriteEntry(_eventSource, message + "\n" + exception.ToString(), System.Diagnostics.EventLogEntryType.Error);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		public static void LogError(Exception exception)
		{
			try
			{
				Console.WriteLine(exception.ToString());
				if (_logger != null)
					_logger.Error(exception.ToString());
				System.Diagnostics.EventLog.WriteEntry(_eventSource, exception.ToString(), System.Diagnostics.EventLogEntryType.Error);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		public static void LogDebug(string message)
		{
			try
			{
				Console.WriteLine(message);
				if (_logger != null)
					_logger.Debug(message);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		public static void LogInfo(string message)
		{
			try
			{
				Console.WriteLine(message);
				if (_logger != null)
					_logger.Info(message);
				System.Diagnostics.EventLog.WriteEntry(_eventSource, message, System.Diagnostics.EventLogEntryType.Information);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		public static void LogWarning(string message)
		{
			try
			{
				Console.WriteLine(message);
				if (_logger != null)
					_logger.Warn(message);
				System.Diagnostics.EventLog.WriteEntry(_eventSource, message, System.Diagnostics.EventLogEntryType.Warning);
			}
			catch (Exception ex)
			{
				//Do Nothing
			}
		}

		#endregion

	}
}